var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");
//任务 看广告赚钱

var task_ad = {
  is_over_today: false,
  go_page_from_home() {
    var logger = utils.create_logger("task_ad go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["视频", "小说"], true)) {
      logger("SUCCESS");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页任务"])
      );
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      if (this.is_over_today) {
        logger("今天已经执行完【看广告赚钱】");
        return;
      }
      this.init();
    } else {
      logger("NOT FOUND");
      this.after();
    }
  },
  click_ad(centerX, centerY) {
    var logger = utils.create_logger("task_ad click_ad: ");
    logger("");
    var DELTA_X = 598 - 127;
    var DELTA_Y = 948 - 926;
    var rate_x = device.width / 720;
    var rate_y = device.height / 1650;
    var x = centerX + DELTA_X * rate_x;
    var y = centerY + DELTA_Y * rate_y;
    utils.click_x_y(parseInt(x), parseInt(y));
  },
  init() {
    in_task.init(null, null, null, null, null, null, 3);
    var logger = utils.create_logger("task_ad init: ");
    logger();
    var ocr_text = ocr_utils.find_text(
      "看广告赚钱",
      false,
      null,
      2,
      parseInt((device.height / 10) * 3)
    );
    if (ocr_text) {
      logger(
        "找到【看广告赚钱】,防止和开宝箱冲突，将向上滑动device.height/10*2"
      );
      swipe(
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 10) * 5,
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 10) * 3,
        utils.get_random_interval(200, 0, 500, 1)
      );
      sleep(utils.get_random_interval(1000, 0, 1000, 1));
      var x = ocr_text.bounds.centerX();
      var y = ocr_text.bounds.centerY() - parseInt((device.height / 10) * 2);
      this.click_ad(x, y);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      if (utils.exists_text("视频") && utils.exists_text("小说")) {
        this.is_over_today = true;
        logger("今天已经执行完【看广告赚钱】");
        this.after();
        return;
      }

      ad.has_watch_ad_times = 0;
      ad.max_ad_times = 1000;
      ad.init(null, null, null, null, 3);
      if (ad.in_task) {
        in_task.init(null, null, null, null, null, null, 3);
      }
      this.after();
      return;
    }
    logger("没找到【看广告赚钱】");
    this.after();
    return;
  },
  after() {
    var logger = utils.create_logger("task_ad after: ");
    logger();
    this.next();
  },
  back_top(times) {
    var logger = utils.create_logger("task_ad back_top: ");
    logger("下面开始回到顶部，将向上滑动--" + times + "--次");
    while (times > 0) {
      swipe(
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 3) * 1 * utils.get_swipe_times(95, 5),
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 3) * 2 * utils.get_swipe_times(95, 5),
        utils.get_random_interval(200, 0, 500, 1)
      );
      times = times - 1;
      console.log("还需要向上滑动--" + times + "--次");
      sleep(utils.get_random_interval(1, 0, 1, 1000));
    }
  },
  next() {
    this.back_top(4);
    console.log("ad_task next");
  },
};

module.exports = task_ad;
